import { makeAutoObservable } from "mobx"
import {setToken, getToken, http, clearToken} from '@/utils'

/**
 * 登录模块store
 * 请求路由封装类
 */
class LoginStore {
  token = getToken() || ''
  constructor() {
    makeAutoObservable(this)
  }
  // 调用登录接口
  login = async ({ mobile, code }) => {
    const res = await http.post('http://geek.itheima.net/v1_0/authorizations', {
      mobile,
      code
    })
    // 把token存入内存中
    this.token = res.data.token
    // 把token存入localStorage 实现token持久化
    setToken(res.data.token)
  }
  // 调用退出登录接口
  loginOut = () => {
    this.token = ''
    clearToken()
  }

}
export default LoginStore